package com.example.planstudio;

import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import es.dmoral.toasty.Toasty;

import java.util.Calendar;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView backHome,delete,saveNote;
    private TextView title,backText;
    private EditText et_Content;
    private MyDBhelper myDBhelper;
    private String sendId;
    private Button dateButton;
    private Button timeButton;
    private Calendar calendar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        init();

        backHome.setOnClickListener(this);
//        delete.setOnClickListener(this);
        saveNote.setOnClickListener(this);
        dateButton.setOnClickListener(this);
        timeButton.setOnClickListener(this);

        Intent intent=this.getIntent();
        sendId=intent.getStringExtra("id");
        if(sendId==null){
//            title.setText("添加数据^.^");
        }else {
//            title.setText("修改数据:)");
//            String time=intent.getStringExtra("Time");
            String event=intent.getStringExtra("Event");
//            showTime.setVisibility(View.VISIBLE);
//            showTime.setText(time);
            et_Content.setText(event);
        }
    }
    private void init(){
        backHome=findViewById(R.id.back1);
        backText=findViewById(R.id.backText1);
//        delete=findViewById(R.id.delete);
        saveNote=findViewById(R.id.save_note);
//        title=findViewById(R.id.title);
//        showTime=findViewById(R.id.showtime);
        dateButton = findViewById(R.id.dateButton);
        timeButton = findViewById(R.id.timeButton);
        calendar = Calendar.getInstance();
        et_Content=findViewById(R.id.content);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back1:
                finish();
                break;
            case R.id.backText1:
                finish();
                break;
//            case R.id.delete:
//                et_Content.setText("");
//                break;
            case R.id.save_note:
                if(sendId==null){
                    String content=et_Content.getText().toString();
                    if(content==""){
                        Toasty.info(RecordActivity.this,"内容不能为空！！",Toast.LENGTH_LONG).show();
                    }else{
                        myDBhelper=new MyDBhelper(RecordActivity.this,"note.db",null,1);
                        Boolean flag=myDBhelper.insertData(content);
                        Intent intent = new Intent(RecordActivity.this, ClockActivity.class);
                        intent.putExtra("msg",content);
                        PendingIntent pend = PendingIntent.getActivity(RecordActivity.this, 111, intent, 0); //显示闹钟，alarmActivity
                        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);       // 通过Context.ALARM_SERVICE获取AlarmManager对象
                        alarm.set(AlarmManager.RTC, calendar.getTimeInMillis(), pend);     //设定闹钟
                        System.out.println(calendar.getTime());
                        if(flag==true){
                            setResult(2);
                            Toasty.success(RecordActivity.this,"添加成功！！",Toast.LENGTH_LONG).show();
                            finish();
                        }else {
                            Toasty.error(RecordActivity.this,"添加失败！！",Toast.LENGTH_LONG).show();
                        }
                    }
                }else {
                    String content=et_Content.getText().toString();
                    if(content==""){
                        Toasty.info(RecordActivity.this,"内容不能为空！！",Toast.LENGTH_LONG).show();
                    }else{
                        myDBhelper=new MyDBhelper(RecordActivity.this,"note.db",null,1);
                        if(myDBhelper.updateData(sendId,content)){
                            setResult(2);
                            Toasty.success(RecordActivity.this,"更新成功！！",Toast.LENGTH_LONG).show();
                            finish();
                        }else {
                            Toasty.error(RecordActivity.this,"更新失败！！",Toast.LENGTH_LONG).show();
                        }
                    }
                }
                break;
            case R.id.dateButton:
                //弹窗
                DatePickerDialog datePickerDialog = new DatePickerDialog(RecordActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                                String date = String.format("%d 年 %d 月 %d 日",year, month, dayOfMonth);
                                dateButton.setText(date);
                            }
                        },calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();

                // 确认按钮
//                datePickerDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(view -> {
//                    toast("positive button");
//                    datePickerDialog.dismiss();
                break;
            case R.id.timeButton:
                //弹窗
                TimePickerDialog timePickerDialog = new TimePickerDialog(RecordActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                String date = String.format("%d 点 %d 分", hourOfDay, minute);
                                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                calendar.set(Calendar.MINUTE, minute);
                                calendar.set(Calendar.SECOND, 0);
                                timeButton.setText(date);
                            }
                        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                timePickerDialog.show();
                break;
        }
    }
}